package com.egg.ylt.presenter.impl;

import android.text.TextUtils;

import com.egg.ylt.Utils.API;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.pojo.CouponEntity;
import com.egg.ylt.pojo.NatatoriumByidEntity;
import com.egg.ylt.pojo.RealtimeDataEntity;
import com.egg.ylt.pojo.ShopDetailCouponEntity;
import com.egg.ylt.view.IShopDetailView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * Created by lijiateng on 2019/2/25.
 */

public class ShopDetailPresenterImpl extends BasePresenter<IShopDetailView> {

    public void getShopDetails(String id) {
        mView.showLoading("数据加载中");

        // 请求优惠券信息
        getCouponInfo(id);

        // 请求门店详情
        getShopDetail(id);
    }

    /**
     * 请求门店详情
     * @param id
     */
    private void getShopDetail(String id) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("id", id);
        hashMap.put("companyId", Constants.COMPANYID);
        hashMap.put("pageSize", "1");
        hashMap.put("pageNum", "20");
        RequestManager.getInstance().requestPostByAsyn(API.GET_NATATORIUM_BY_ID, hashMap, new ReqCallBack<NatatoriumByidEntity>() {

            @Override
            public void onReqSuccess(NatatoriumByidEntity result) {
                mView.hideLoading();
                mView.delayInflateViews();
                mView.getShopDetail(result);
            }

            @Override
            public void onFailure(String result) {
                mView.hideLoading();
                mView.delayInflateViews();
                mView.showNetError();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.hideLoading();
                mView.delayInflateViews();
                mView.showNetError();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    public void updateAirAndWaterQuality() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("companyId", Constants.COMPANYID);
        RequestManager.getInstance().requestPostByAsyn(API.POST_REALTIME_DATA, hashMap, new ReqCallBack<RealtimeDataEntity>() {

            @Override
            public void onReqSuccess(RealtimeDataEntity result) {
                mView.updateAirWaterStatus(result);
            }

            @Override
            public void onFailure(String result) {
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    public void setCancelMessage(String token, String shopId) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("messageId", shopId);
        RequestManager.getInstance().requestPostByAsyn(API.POST_CANCEL_MESSAGE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.onCollectedChanged(false);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    public void setCollectMessage(String token, String shopId,String companyId) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("messageId", shopId);
        hashMap.put("companyId", companyId);
        //0 为咨询标识，1为门店详情
        hashMap.put("collectType","1");
        RequestManager.getInstance().requestPostByAsyn(API.POST_COLLECT_MESSAGE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.onCollectedChanged(true);
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });

    }

    /**
     * 获取优惠券列表
     */
    public void getCouponInfo(String shopId) {

        if (!isUserLogin()) {
            return;
        }

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("orgId", shopId);
        RequestManager.getInstance().requestPostByAsyn(API.QUERY_RECEIVED_COUPON, hashMap, new ReqCallBack<ShopDetailCouponEntity>() {
            @Override
            public void onReqSuccess(ShopDetailCouponEntity result) {

                if (result != null) {
                    mView.updateCouponList(result);
                }
            }

            @Override
            public void onFailure(String result) {
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    public boolean isUserLogin() {
        return !TextUtils.isEmpty(Constants.TOKEN);
    }

    public void requestObtainCoupon(CouponEntity.ListEntity model, final int position) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", Constants.TOKEN);
        hashMap.put("memberCompanyId", model.getMemberCompanyId());
        hashMap.put("ticketId", model.getTicketId());
        hashMap.put("activityId", model.getActivityId());
        hashMap.put("id", model.getId());
        RequestManager.getInstance().requestPostByAsyn(API.RECEIVE_COUPON, hashMap, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                mView.updateAfterCouponStatusChanged(position);
            }

            @Override
            public void onFailure(String result) {
                CommonUtils.makeEventToast(mContext, "领取失败，请重新尝试", false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                CommonUtils.makeEventToast(mContext, "领取失败，请重新尝试", false);
            }
        });
    }

}
